<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Show_entity{
	var $id = 0;
	var $user_created_id = 0;
	var $user_bid_won = 0;
	var $name = "";
	var $ref_no = "";
	var $show_type = "";
	var $bid_type = "";
	var $show_duration = "";
	var $artist = "";
	var $country_origin_code = "";
	var $music_genre = "";
	var $speaker_profile = "";
	var $plays_musicals = "";
	var $start_date = 0;
	var $end_date = 0;
	var $bid_due_date = 0;
	var $available_for = "";
	var $entourage_no = 0;
	var $min_bid = 0;
	var $air_travel = "";
	var $ground = "";
	var $hotel = "";
	var $per_diem = "";
	var $light_n_sound = "";
	var $rider_req = "";
	var $visa_n_work_permits = "";
	var $additional = "";
	var $tech_rider = "";
	var $show_image_1 = "";
	var $show_image_2 = "";
	var $show_image_3 = "";
	var $hospitality_rider = "";
	var $terms_n_conditions = "";
	var $status = "opening";
	var $pay_status = "";
	var $created_time = 0;
	var $revision = "";

	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_SHOW)->result();
		}
		else if($cmd == 'ref_no')
		{
			$result = $ci->db->where('ref_no',$input)->get(TBL_SHOW)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->user_created_id = $result[0]->user_created_id;
			$this->user_bid_won = $result[0]->user_bid_won;
			$this->name = $result[0]->name;
			$this->ref_no = $result[0]->ref_no;
			$this->show_type = $result[0]->show_type;
			$this->show_duration = $result[0]->show_duration;
			$this->bid_type = $result[0]->bid_type;
			$this->artist = $result[0]->artist;
			$this->country_origin_code = $result[0]->country_origin_code;
			$this->music_genre = $result[0]->music_genre;
			$this->speaker_profile = $result[0]->speaker_profile;
			$this->plays_musicals = $result[0]->plays_musicals;
			$this->start_date = $result[0]->start_date;
			$this->end_date = $result[0]->end_date;
			$this->bid_due_date = $result[0]->bid_due_date;
			$this->available_for = $result[0]->available_for;
			$this->entourage_no = $result[0]->entourage_no;
			$this->min_bid = $result[0]->min_bid;
			$this->air_travel = $result[0]->air_travel;
			$this->ground = $result[0]->ground;
			$this->hotel = $result[0]->hotel;
			$this->per_diem = $result[0]->per_diem;
			$this->light_n_sound = $result[0]->light_n_sound;
			$this->rider_req = $result[0]->rider_req;
			$this->visa_n_work_permits = $result[0]->visa_n_work_permits;
			$this->additional = $result[0]->additional;
			$this->tech_rider = $result[0]->tech_rider;
			$this->show_image_1 = $result[0]->show_image_1;
			$this->show_image_2 = $result[0]->show_image_2;
			$this->show_image_3 = $result[0]->show_image_3;
			$this->hospitality_rider = $result[0]->hospitality_rider;
			$this->terms_n_conditions = $result[0]->terms_n_conditions;
			$this->status = $result[0]->status;
			$this->pay_status = $result[0]->pay_status;
			$this->created_time = $result[0]->created_time;
			$this->revision = $result[0]->revision;
			
			return $this;
		}
		return false;
	}
	
	function setUserCreatedId($input){ $this->user_created_id=$input; }
	function setUserBidWon($input){ $this->user_bid_won= $input;  }
	function setName($input){ $this->name=$input; }
	function setRefNo($input){ $this->ref_no=$input; }
	function setShowType($input){ $this->show_type=$input; }
	function setShowDuration($input){ $this->show_duration=$input; }
	function setBidType($input) { $this->bid_type=$input; }
	function setArtist($input){ $this->artist=$input; }
	function setCountryOriginCode($input){ $this->country_origin_code=$input; }
	function setMusicGenre($input){ $this->music_genre=$input; }
	function setSpeakerProfile($input){ $this->speaker_profile=$input; }
	function setPlaysMusicals($input){ $this->plays_musicals=$input; }
	function setStartDate($input){ $this->start_date=$input; }
	function setEndDate($input){ $this->end_date = $input; }
	function setBidDueDate($input) { $this->bid_due_date = $input; }
	function setAvailableFor($input){ $this->available_for=$input; }
	function setEntourageNo($input){ $this->entourage_no=$input; }
	function setMinBid($input) { $this->min_bid=$input; }
	function setAirTravel($input){ $this->air_travel=$input; }
	function setGround($input){ $this->ground=$input; }
	function setHotel($input){ $this->hotel=$input; }
	function setPerDiem($input){ $this->per_diem=$input; }
	function setLightAndSound($input) { $this->light_n_sound=$input; }
	function setRiderReq($input) { $this->rider_req=$input; }
	function setVisaAndWorkPermits($input) { $this->visa_n_work_permits=$input; }
	function setAdditional($input) { $this->additional=$input; }
	function setTechRider($input) {$this->tech_rider = $input; }
	function setShowImage1($input) { $this->show_image_1=$input; }
	function setShowImage2($input) { $this->show_image_2 = $input; }
	function setShowImage3($input) { $this->show_image_3 = $input; }
	function setHospitalityRider($input) { $this->hospitality_rider =$input; }
	function setTermsAndConditions($input) { $this->terms_n_conditions =$input; }
	function setStatus($input) { $this->status= $input; }
	function setPayStatus($input) { $this->pay_status= $input; }
	function setCreatedTime($input) { $this->created_time= $input; }
	function setRevision($input){ $this->revision = $input; }
	
	function getId(){return $this->id;}
	function getUserCreatedId(){ return $this->user_created_id; }
	function getUserBidWon(){ return $this->user_bid_won; }
	function getName(){ return $this->name; }
	function getRefNo(){ return $this->ref_no; }
	function getShowType() { return $this->show_type;}
	function getShowDuration() { return $this->show_duration;}
	function getBidType(){ return $this->bid_type; }
	function getArtist(){ return $this->artist; }
	function getCountryOriginCode(){ return $this->country_origin_code; }
	function getMusicGenre(){ return $this->music_genre; }
	function getSpeakerProfile(){ return $this->speaker_profile; }
	function getPlaysMusicals(){ return $this->plays_musicals; }	
	function getStartDate(){ return $this->start_date; }
	function getEndDate(){ return $this->end_date; }
	function getBidDueDate() { return $this->bid_due_date; }
	function getAvailableFor(){ return $this->available_for; }
	function getEntourageNo(){ return $this->entourage_no; }
	function getMinBid(){ return $this->min_bid; }
	function getAirTravel(){ return $this->air_travel; }
	function getGround(){ return $this->ground; }
	function getHotel(){ return $this->hotel; }
	function getPerDiem(){ return $this->per_diem; }
	function getLightAndSound(){ return $this->light_n_sound; }
	function getRiderReq() { return $this->rider_req; }
	function getVisaAndWorkPermits() { return $this->visa_n_work_permits; }
	function getAdditional() { return $this->additional; }
	function getTechRider() { return $this->tech_rider; }
	function getShowImage1() {return $this->show_image_1; }
	function getShowImage2() { return $this->show_image_2; }
	function getShowImage3(){ return $this->show_image_3; }
	function getHospitalityRider() { return $this->hospitality_rider; } 
	function getTermsAndConditions() { return $this->terms_n_conditions; }
	function getStatus() { return $this->status; }
	function getPayStatus() { return $this->pay_status; }
	function getCreatedTime() { return $this->created_time; }
	function getRevision(){ return $this->revision; }
	
	function update()
	{
		$ci=& get_instance();
		
		$ci->db->set('user_created_id', $this->user_created_id);
		$ci->db->set('user_bid_won', $this->user_bid_won);
		$ci->db->set('name', $this->name);
		$ci->db->set('ref_no', $this->ref_no);
		$ci->db->set('show_type', $this->show_type);
		$ci->db->set('show_duration', $this->show_duration);
		$ci->db->set('bid_type', $this->bid_type);
		$ci->db->set('artist', $this->artist);
		$ci->db->set('country_origin_code', $this->country_origin_code);
		$ci->db->set('music_genre', $this->music_genre);
		$ci->db->set('speaker_profile', $this->speaker_profile);
		$ci->db->set('plays_musicals', $this->plays_musicals);
		$ci->db->set('start_date', $this->start_date);
		$ci->db->set('end_date', $this->end_date);
		$ci->db->set('bid_due_date', $this->bid_due_date );
		$ci->db->set('available_for', $this->available_for);
		$ci->db->set('entourage_no', $this->entourage_no);
		$ci->db->set('min_bid', $this->min_bid);
		$ci->db->set('air_travel', $this->air_travel);
		$ci->db->set('ground', $this->ground);
		$ci->db->set('hotel', $this->hotel);
		$ci->db->set('per_diem', $this->per_diem);
		$ci->db->set('light_n_sound', $this->light_n_sound);
		$ci->db->set('rider_req', $this->rider_req);
		$ci->db->set('visa_n_work_permits', $this->visa_n_work_permits);
		$ci->db->set('additional', $this->additional);
		$ci->db->set('tech_rider', $this->tech_rider);
		$ci->db->set('show_image_1', $this->show_image_1);
		$ci->db->set('show_image_2', $this->show_image_2);
		$ci->db->set('show_image_3', $this->show_image_3);
		$ci->db->set('hospitality_rider', $this->hospitality_rider);
		$ci->db->set('terms_n_conditions', $this->terms_n_conditions);
		$ci->db->set('status', $this->status);
		$ci->db->set('pay_status', $this->pay_status);
		$ci->db->set('created_time', $this->created_time);
		$ci->db->set('revision', $this->revision );
		
		$ci->db->where('id', $this->id)->update(TBL_SHOW);
	}
	
	function createNewShowEntity($refNo){
		$ci=& get_instance();
		
		$ci->db->set('ref_no',$refNo);
		$ci->db->insert(TBL_SHOW);
		
		return $ci->db->insert_id();
	}
	
	function changeStatusOfShowOutOfBid(){
		$ci=& get_instance();
		$ci->db->query("UPDATE ".TBL_SHOW." SET status='cancelled' WHERE status='opening' AND bid_due_date<".time()."");
		$ci->db->query("UPDATE ".TBL_SHOW." SET user_bid_won=NULL WHERE status !='closed' ");
	}
	
	function buildNewRevision(){
		$old_revision = $this->revision;
		
		$revison_array = array();
		if(!empty($old_revision)){
			$tmp_array = unserialize($old_revision);
			$tmp_array = array_reverse($tmp_array);
			$i= 0;
			foreach($tmp_array as $array){
				if($i>3){break;}
				$i++;
				$revison_array[] = $array;
			}
			$revison_array = array_reverse($revison_array);
		}
	
		$new_revision = array(
			 'id' => $this->id,
			 'name' => $this->name,
			 'user_created_id' => $this->user_created_id,
			 'user_bid_won' => $this->user_bid_won,
			 'ref_no' => $this->ref_no,
			 'show_type' => $this->show_type,
			 'show_duration' => $this->show_duration,
			 'bid_type' => $this->bid_type,
			 'artist' => $this->artist,
			 'country_origin_code' => $this->country_origin_code,
			 'music_genre' => $this->music_genre,
			 'speaker_profile' => $this->speaker_profile,
			 'plays_musicals' => $this->plays_musicals,
			 'start_date' => $this->start_date,
			 'end_date' => $this->end_date, 
			 'bid_due_date' => $this->bid_due_date,
			 'available_for' => $this->available_for,
			 'entourage_no' => $this->entourage_no,
			 'min_bid' => $this->min_bid,
			 'air_travel' => $this->air_travel,
			 'ground' => $this->ground,
			 'hotel' => $this->hotel,
			 'per_diem' => $this->per_diem,
			 'light_n_sound' => $this->light_n_sound,
			 'rider_req' => $this->rider_req,
			 'visa_n_work_permits' => $this->visa_n_work_permits,
			 'additional' => $this->additional,
			 'tech_rider' => $this->tech_rider,
			 'show_image_1' => $this->show_image_1,
			 'show_image_2' => $this->show_image_2,
			 'show_image_3' => $this->show_image_3,
			 'hospitality_rider' => $this->hospitality_rider,
			 'terms_n_conditions' => $this->terms_n_conditions,
			 'status' => $this->status,
			 'pay_status' => $this->pay_status,
			 'created_time' => $this->created_time
		);
		
		$revison_array[] = $new_revision;
		return serialize( $revison_array );
	}
	
	function countHowManyUserBidThisShow(){
		$ci=& get_instance();
		return count($ci->db->where('show_ref_no',$this->ref_no)->get(TBL_BID)->result());
	}
	
	function makescrid(){
		return md5($this->getRefNo()."&".$this->getId());
	}
}
